package com.itheima.tlias.Controller;

import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController

public class UpLoadController {

//
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//
//        log.info("文件上传" + username + " " + age + " " + image);
//        log.info("测试断点是否能都截止");
//
//        String OriginalFilename = image.getOriginalFilename();
//        int index = OriginalFilename.lastIndexOf(".");
//        String uuid=UUID.randomUUID().toString();
//        String extent = OriginalFilename.substring(index);
//        String newFileName = uuid+ extent;
//        //接收文件存储在服务器目录下
//        log.info(newFileName);
//
//        image.transferTo(new File("E:\\images\\" + newFileName));
//        return Result.success();
//    }
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {

        log.info("文件上传"+image.getOriginalFilename());

        //调用阿里云
        String url=aliOSSUtils.upload(image);

        log.info("文件上传完成"+url);

        return Result.success(url);
    }

}
